﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Microsoft.Win32;
using System.Security.Permissions;
using ThousandFunClient.model;
using ThousandFunClient.Util;
using System.Collections;
using System.IO.Ports;
using System.Timers;
using log4net;
using System.Globalization;
using System.Threading;

namespace ThousandFunClient
{
    public partial class Fm_About : Form
    {
        private string infor = "";

        public string Infor
        {
            get { return infor; }
            set { infor = value; }
        }

        public Fm_About()
        {
            InitializeComponent();
        }

        private void Fm_About_Load(object sender, EventArgs e)
        {
            reg_txt.Text = "本机mac:"+ConfigUtil.GetMacAddress();
            lbl_about.AutoSize = true;
            lbl_about.MaximumSize = new Size(240, 0);
            string cc = System.Configuration.ConfigurationSettings.AppSettings["aboutTXT"];
            lbl_about.Text = cc;// "Xxx软件，V1.0, 2012 \n 上海动纳动力科技有限公司 \n 上海市张杨路88号601室 \n 电话: 021-5877 0523 \n 邮箱: Info@dynapowertech.com \n 网址: www.dynapowertech.com";
            label1.Text = System.Configuration.ConfigurationSettings.AppSettings["aboutTXT1"];
            label2.Text = System.Configuration.ConfigurationSettings.AppSettings["aboutTXT2"];
            label3.Text = System.Configuration.ConfigurationSettings.AppSettings["aboutTXT3"];
            label4.Text = System.Configuration.ConfigurationSettings.AppSettings["aboutTXT4"];
            label5.Text = System.Configuration.ConfigurationSettings.AppSettings["aboutTXT5"];
            if (infor.Equals("1"))
            {
                btn_back.Visible = false;
                reg_txt.Visible = true;
                reg_btn.Visible = true;
                if (Application.ProductVersion.Equals("0.0.0.0"))
                {
                    lbl_versioninfo.Text = "您使用的试用版本已过期,请联系上海动纳动力科技有限公司";
                }
                else
                {
                    lbl_versioninfo.Text = "您使用的版本已过期,请联系上海动纳动力科技有限公司";
                }

            }
            else
            {
                int count = 0;
                int loginCount = 0;
                string loginTime = "";
                string version = "";
                DateTime dt = System.DateTime.Now.AddDays(+45d);
                string timespan = string.Format("{0:yyyyMMdd}", dt);
                RegistryKey mainkey = Registry.LocalMachine;
                RegistryKey subkey = mainkey.OpenSubKey("SOFTWARE\\Register", true);
                string regstr = CryptographyUtil.Encrypt(timespan + ConfigUtil.GetMacAddress());
                if (subkey == null)
                {
                    subkey = mainkey.CreateSubKey("SOFTWARE\\Register");
                    subkey.SetValue("使用次数", count);
                    subkey.SetValue("使用时间", regstr);
                    subkey.SetValue("使用版本", "0.0.0.0");
                }
                else
                {
                    loginCount = (int)subkey.GetValue("使用次数");
                    loginTime = (string)subkey.GetValue("使用时间");
                    version = (string)subkey.GetValue("使用版本");
                }
                string decryptstr = CryptographyUtil.Decrypt(loginTime);
                string timereg = decryptstr.Substring(0, 8);
                lbl_versioninfo.Text = "您使用的版本过期时间为" + timereg ;
            }
        }

        private void btn_back_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void reg_btn_Click(object sender, EventArgs e)
        {

            string message = "请仔细核对,确认输入注册码?";
            string caption = "输入注册码";
            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
            DialogResult result;

            // Displays the MessageBox.

            result = MessageBox.Show(this, message, caption, buttons);

            if (result == DialogResult.No)
            {

                return;

            }
 
            string regstrinput = reg_txt.Text.Trim();
            string versionnow = Application.ProductVersion;


            int count = 0;
            int loginCount = 0;
            string loginTime = "";
            string version = "";
            DateTime dt = System.DateTime.Now.AddDays(+45d);
            string timespan = string.Format("{0:yyyyMMdd}", dt);


            //HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
            RegistryKey mainkey = Registry.LocalMachine;
            RegistryKey subkey = mainkey.OpenSubKey("SOFTWARE\\Register", true);

            string regstr = CryptographyUtil.Encrypt(timespan + ConfigUtil.GetMacAddress());

            if (subkey == null)
            {
                subkey = mainkey.CreateSubKey("SOFTWARE\\Register");
                subkey.SetValue("使用次数", count);
                subkey.SetValue("使用时间", regstr);
                subkey.SetValue("使用版本", "0.0.0.0");
            }
            else
            {
                loginCount = (int)subkey.GetValue("使用次数");
                loginTime = (string)subkey.GetValue("使用时间");
                version = (string)subkey.GetValue("使用版本");
            }

            subkey.SetValue("使用时间", regstrinput);
            subkey.SetValue("使用版本", versionnow);
            MessageBox.Show("输入注册信息成功,请重启程序");
            Application.Exit();

        }
    }
}
